﻿using _8Group.MES.IAppService.Location;
using _8Group.MES.IAppService.Location.Dtos;
using _8Group.MES.IAppService.Warehouses;
using _8Group.MES.IAppService.Warehouses.Dtos;
using _8Group.MES.Model.WM仓储管理;
using FourGroups.Project;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;

namespace _8Group.MES.AppService
{
    /// <summary>
    /// 库区实现
    /// </summary>
    [ApiExplorerSettings(GroupName = "周扬")]
    public class LocationAppService : MESAppService, ILocationAppService
    {
        private readonly IRepository<WM_Storage_Location, int> _locationRepository;

        public LocationAppService(IRepository<WM_Storage_Location, int> locationRepository)
        {
            _locationRepository = locationRepository;
        }

        /// <summary>
        /// 添加库区
        /// </summary>
        /// <param name="input">库区</param>
        /// <returns></returns>
        public async Task<int> AddLocation(CreateLocationDto input)
        {
            var location = AutoMapperExt.MapTo<WM_Storage_Location>(input);
            location.CreateBy = "admin";
            location.CreateTime = DateTime.Now;
            await _locationRepository.InsertAsync(location);
            return 1;
        }

        /// <summary>
        /// 修改库区
        /// </summary>
        /// <param name="input">库区</param>
        /// <returns></returns>
        public async Task<bool> UpdateLocation(UpdateLocationDto input)
        {
            var location = AutoMapperExt.MapTo<WM_Storage_Location>(input);
            location.UpdateBy = "admin";
            location.UpdateTime = DateTime.Now;
            await _locationRepository.UpdateAsync(location);
            return true;
        }

        /// <summary>
        /// 删除库区
        /// </summary>
        /// <param name="id">库区编号</param>
        /// <returns></returns>
        public async Task<bool> DeleteLocation(int id)
        {
            var location = await _locationRepository.GetAsync(id);
            await _locationRepository.DeleteAsync(location, true);
            return true;
        }

        /// <summary>
        /// 显示库区信息
        /// </summary>
        /// <returns></returns>
        public async Task<string> GeLocations(string? locationName, int pageIndex = 1, int pageSize = 10)
        {
            var locations = await _locationRepository.GetQueryableAsync();

            locations = locations.WhereIf(!string.IsNullOrWhiteSpace(locationName), x => x.LocationName.Contains(locationName));

            int totalCount = locations.Count();
            locations = locations.OrderByDescending(x => x.CreateTime).Skip((pageIndex - 1) * pageSize).Take(pageSize);

            var result = new ResponseModel
            {
                data = locations.ToList(), 
                code = "0",
                count = totalCount,
                msg = "请求成功"
            };

            return JsonConvert.SerializeObject(result, Formatting.Indented);
        }
    }
}
